package com.sl.hx.shop.ui.home.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.ui.home.adapter.BaseFragmentAdapter;
import com.sl.hx.shop.ui.home.fragment.FlashSaleFragment;
import com.sl.hx.shop.utils.TimeUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 限时抢购页
 */

public class FlashSaleActivity extends BaseActivity{


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    @BindView(R.id.viewpage)
    ViewPager viewpager;

    private BaseFragmentAdapter mFragmentAdapter;
    private String nowTime;
    private String [] times;

    private String [] titleTime = {"00:00", "10:00", "12:00", "14:00", "18:00", "22:00"};
    //private String [] states = {"已开抢", "已开抢", "疯抢中", "即将开始", "疯抢中", "即将开始"};
    private String [] states;
    private int mHour;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_flashsale;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {

        initDate();
        initTabLayout();
    }

    private void initDate() {
        //mHour 当前时间  小时
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        mHour=cal.get(Calendar.HOUR_OF_DAY); //24小时制 HOUR_OF_DAY

        //times 用来请求的时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        nowTime=sdf.format(new Date());
        times  = new String[]{nowTime+" 00:00:00", nowTime+" 10:00:00", nowTime+" 12:00:00", nowTime+" 14:00:00", nowTime+" 18:00:00", nowTime+" 22:00:00",nowTime+" 24:00:00"};
        //tablayout显示的状态(已开抢,抢购中,即将开始)
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime=sdf1.format(new Date());
        states = new String[]{TimeUtil.isDateOneBigger(currentTime,times[0]),TimeUtil.isDateOneBigger(currentTime,times[1]),TimeUtil.isDateOneBigger(currentTime,times[2]),
                TimeUtil.isDateOneBigger(currentTime,times[3]),TimeUtil.isDateOneBigger(currentTime,times[4]),TimeUtil.isDateOneBigger(currentTime,times[5]),TimeUtil.isDateOneBigger(currentTime,times[6])};
    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.iv_back, R.id.tablayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tablayout:
                break;
        }
    }

    private void initTabLayout() {
        List<String> mNameList = new ArrayList<>();
        List<Fragment> mFragmentList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            mNameList.add(titleTime.toString());
            mFragmentList.add(createListFragments(times[i],times[i+1]));
        }
        if(mFragmentAdapter==null) {
            mFragmentAdapter = new BaseFragmentAdapter(getSupportFragmentManager(), mFragmentList,mNameList);
        }else{
            //刷新fragment
            mFragmentAdapter.setFragments(getSupportFragmentManager(),mFragmentList,mNameList);
        }
        viewpager.setAdapter(mFragmentAdapter);
        tabLayout.setupWithViewPager(viewpager);
        viewpager.setOffscreenPageLimit(mNameList.size());
        setCurrent();//默认选中的item

        for (int i = 0; i < mFragmentAdapter.getCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);//获得每一个tab
            tab.setCustomView(R.layout.tablayout_item);//给每一个tab设置view
            if (i == 0) {
                //设置第一个tab的TextView是被选择的样式
                tab.getCustomView().findViewById(R.id.ll_layoutitem).setSelected(true);//第一个tab被选中
            }
            TextView time =  tab.getCustomView().findViewById(R.id.time);
            TextView state =  tab.getCustomView().findViewById(R.id.state);
            time.setText(titleTime[i]);//设置tab上的文字
            state.setText(states[i]);
        }
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.time).setSelected(true);
                tab.getCustomView().findViewById(R.id.state).setSelected(true);
                tab.getCustomView().findViewById(R.id.ll_layoutitem).setSelected(true);
                viewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.time).setSelected(false);
                tab.getCustomView().findViewById(R.id.state).setSelected(false);
                tab.getCustomView().findViewById(R.id.ll_layoutitem).setSelected(false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void setCurrent() {
        if (mHour<10) {
            viewpager.setCurrentItem(0);
        }else if (mHour>=10&&mHour<12){
            viewpager.setCurrentItem(1);
        }else if (mHour>=12&&mHour<14){
            viewpager.setCurrentItem(2);
        }else if (mHour>=14&&mHour<18){
            viewpager.setCurrentItem(3);
        }else if (mHour>=18&&mHour<22){
            viewpager.setCurrentItem(4);
        }else if (mHour>=22){
            viewpager.setCurrentItem(5);
        }
    }


    private FlashSaleFragment createListFragments(String starttime, String endtime) {
        FlashSaleFragment fragment=new FlashSaleFragment();
        Bundle bundle =new Bundle();
        bundle.putString("flashSaleStart",starttime);
        bundle.putString("flashSaleEnd",endtime);
        fragment.setArguments(bundle);
        return fragment;
    }
}
